/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：BattleScreen.java <br>
 * 创建时间：2013-7-30 下午4:06:28 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2.battle;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;

import lostland.gmud.exv2.Assets;
import lostland.gmud.exv2.FontSize;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.netbattle.BattleInfo;
import lostland.gmud.exv2.battle.netbattle.ClientViewScreen;
import lostland.gmud.exv2.battle.netbattle.NetworkMode;
import lostland.gmud.exv2.battle.proc.BattleStart;
import lostland.gmud.exv2.battle.proc.FreeStatus;
import lostland.gmud.exv2.battle.proc.PreAttackStatus;
import lostland.gmud.exv2.battle.proc.Status;
import lostland.gmud.exv2.battle.proc.holder.BattleHolder;
import lostland.gmud.exv2.battle.proc.holder.CommonBattle;
import lostland.gmud.exv2.blgframework.CScreen;
import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.blgframework.impl.BLGPixmap;
import lostland.gmud.exv2.data.Gesture;
import lostland.gmud.exv2.data.Item;
import lostland.gmud.exv2.data.Npc;
import lostland.gmud.exv2.data.NpcBattleData;
import lostland.gmud.exv2.data.Room;

/**
 * 类名：BattleScreen <br>
 * 说明：战斗主界面
 *
 * @author 12548
 */
public class BattleScreen extends CScreen {

	public NetworkMode networkMode = NetworkMode.LOCAL;
	public BattleInfo battleInfo = null;
	private volatile BattleStateInfo bsi = null;
	public int idstamp = 0;
	public String uuid = "";
	public String arena = "";

	public NpcBattleData bd1, bd2;

	public BattleHolder holder;
	public int leftid = 0;
	public int enemyid = 0;
	public int bossid = -1;
	public boolean eob = false, fob = true;
	public boolean trust = false;
	public boolean qkdny1 = false; //乾大绝招闪展腾挪状态

	public static boolean wflag = false;

	/**
	 * @return bs
	 */
	public static BattleScreen getInstance() {
		if (_instance == null) {
			_instance = new BattleScreen();
		}
		return _instance;
	}

	public static void setWFlag(boolean flag) {
		wflag = flag;
	}


	public ArrayList<Status> stack;

	private BattleScreen() {
		super();
		stack = new ArrayList<>(3);
		bd1 = new NpcBattleData();
		bd2 = new NpcBattleData();
	}


	private static BattleScreen _instance;

	public void check0sp(Npc p) {
		Logger.d("%s 体力为0， 是否已复活为%b", p.name, p.getBattleData().alreadyRespawned);
		if (p.haveTalent(124) && !p.getBattleData().alreadyRespawned) { // FIXME
			Logger.d("%s 正在复活", p.name);
			//天赋 信春哥
			p.refresh();
			p.getBattleData().alreadyRespawned = true;
			new ViewScreen(p.name + "信春哥，满血满魔原地复活！").pushToGame();
		} else {
			eob = true;
		}
	}

	@Override
	public void update(float deltaTime) {
//		ttime += deltaTime;
//		if(ttime < delay) return;

		if (networkMode == NetworkMode.CLIENT) {
			new ClientViewScreen().pushToGame();
		} else {
			while (true) {
				if (this != getGame().getCurrentScreen()) return;
				if (eob) {
					BattleOverScreen.setZDFlag(false);
					new BattleOverScreen().replaceCurrent();
					return;
				}
				pop().execute();
				if (!eob) {
					if(p1().getSp() <= 0) {
						check0sp(p1());
					}

					if(p2().getSp() <= 0) {
						check0sp(p2());
					}
				}
			}
		}
	}


	@Override
	public void present(float deltaTime) {

		final int X1 = 25;
		final int Y1 = 66;
		final int DX = 23;

		final int X2 = 208;

		final int DX2 = 12;


		int i, j;

		BLGPixmap pixmap = (BLGPixmap) Assets.getMainchar();
		if (Gmud.mc.sex > 0) {
			pixmap = (BLGPixmap) Assets.getGirl();
		}


		BLGGraphics g = (BLGGraphics) getGame().getGraphics();
		g.clear(Gmud.getBgColor());

		Npc left = p1();
		Npc right = p2();


		int leftfp = left.getFp();
		int leftMaxfp = left.getAttr(Room.ATTR_MAXFP);
		int leftsp = left.getSp();
		int lefthp = left.getHp();
		int leftMaxhp = left.getAttr(Room.ATTR_MAXHP);
		int leftGauge = left.getGauge();

		int rightsp = right.getSp();
		int rightGauge = right.getGauge();
		int rightMaxfp = right.getAttr(Room.ATTR_MAXFP);
		int rightfp = right.getFp();
		int righthp = right.getHp();
		int rightMaxhp = right.getAttr(Room.ATTR_MAXHP);

		if (networkMode == NetworkMode.CLIENT) {
			if (bsi == null) {
				bsi = BattleStateInfo.getState();
			}

			leftfp = bsi.client_fp;
			leftGauge = bsi.client_gauge;
			lefthp = bsi.client_hp;
			leftMaxfp = bsi.client_maxfp;
			leftMaxhp = bsi.client_maxhp;
			leftsp = bsi.client_sp;

			rightfp = bsi.host_fp;
			rightGauge = bsi.host_gauge;
			righthp = bsi.host_hp;
			rightMaxfp = bsi.host_maxfp;
			rightMaxhp = bsi.host_maxhp;
			rightsp = bsi.host_sp;

		}


		if (wflag) {
			if (left.imgindex.x < 0 || left.imgindex.y < 0) {
				pixmap = left.sex == 0 ? (BLGPixmap) Assets.getMainchar() : (BLGPixmap) Assets.getGirl();
				g.drawPixmap(pixmap, 64 + DX2, 32, 0, 0, 32, 32);
			} else {
				left.draw(2, 1, DX2, 0);
			}
			g.drawText(left.name, 60 + DX2, 18, FontSize.FT_12PX);
		} else {
			g.drawPixmap(pixmap, 64 + DX2, 32, 0, 0, 32, 32);
			if (networkMode == NetworkMode.CLIENT) {
				g.drawText(left.name, 60 + DX2, 18, FontSize.FT_12PX);
			} else {
				g.drawText(BattleStart.name, 60 + DX2, 18, FontSize.FT_12PX);
			}

			//你的状态数据
			if (leftMaxfp > 0) {
				g.drawText(leftfp + "/" + leftMaxfp, 130 + DX2, Y1 + 16, FontSize.FT_12PX);
			} else {
				g.drawText("0", 130 + DX2, Y1 + 16, FontSize.FT_12PX);
			}
			g.drawText(leftsp + "/" + lefthp, 130 + DX2, Y1 - 1, FontSize.FT_12PX);
		}

		//场面进度指示条（左边）

		for (i = X1 + DX; i <= X1 + DX
				+ (64 * lefthp / leftMaxhp); i++) {
			g.drawPixel(i + DX2, Y1 + 13, 0);
		}
		for (i = X1 + DX; i <= X1 + DX
				+ (64 * leftsp / leftMaxhp); i++) {
			for (j = Y1; j <= Y1 + 10; j++) {
				g.drawPixel(i + DX2, j, 0);
			}
		}

		if (leftMaxfp > 0) {
			for (i = X1 + DX; i <= X1 + DX
					+ (64 * (leftfp < leftMaxfp ? leftfp
					: leftMaxfp) / leftMaxfp); i++) {
				for (j = Y1 + 16; j <= Y1 + 26; j++) {
					g.drawPixel(i + DX2, j, 0);
				}
			}
			for (i = X1 + DX; i <= X1 + DX + 64; i++) {
				g.drawPixel(i + DX2, Y1 + 29, 0);
			}
		}

//		//木鸡
//		if(left.getDz() > 0)
//			g.drawPixmap(Assets.transfixed, 55, 30);

		int y = 99;
		if (networkMode == NetworkMode.LOCAL) {
			if (left.getStr_bouns() != 0) {
				String att = left.getStr_bouns() > 0 ? "膂力+" : "膂力";
				g.drawText(att + left.getStr_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getAgi_bouns() != 0) {
				String att = left.getAgi_bouns() > 0 ? "敏捷+" : "敏捷";
				g.drawText(att + left.getAgi_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getWxg_bouns() != 0) {
				String att = left.getWxg_bouns() > 0 ? "悟性+" : "悟性";
				g.drawText(att + left.getWxg_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getBon_bouns() != 0) {
				String att = left.getBon_bouns() > 0 ? "根骨+" : "根骨";
				g.drawText(att + left.getBon_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getAtk_bouns() != 0) {
				String att = left.getAtk_bouns() > 0 ? "攻击+" : "攻击";
				g.drawText(att + left.getAtk_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getDef_bouns() != 0) {
				String att = left.getDef_bouns() > 0 ? "防御+" : "防御";
				g.drawText(att + left.getDef_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getHit_bouns() != 0) {
				String att = left.getHit_bouns() > 0 ? "命中+" : "命中";
				g.drawText(att + left.getHit_bouns(), 20 + DX2, y, FontSize.FT_12PX);
				y += 12;
			}
			if (left.getEvd_bouns() != 0) {
				String att = left.getEvd_bouns() > 0 ? "回避+" : "回避";
				g.drawText(att + left.getEvd_bouns(), 20 + DX2, y, FontSize.FT_12PX);
			}
		}
		g.drawPixmap(Assets.vs, 150 + DX2, 44);
		g.drawPixmap(Assets.hpfp, 25 + DX2, 64);

		if (right.imgindex.x < 0 || right.imgindex.y < 0) {
			pixmap = right.sex == 0 ? (BLGPixmap) Assets.getMainchar() : (BLGPixmap) Assets.getGirl();
			g.drawPixmap(pixmap, 224 + DX2, 32, 0, 0, 32, 32);
		} else {
			right.draw(7, 1, DX2, 0);
		}
		g.drawText(right.name, 220 + DX2, 18, FontSize.FT_12PX);

		//场面进度指示条（右边）
		for (i = X2; i <= X2
				+ (64 * righthp / rightMaxhp); i++) {
			g.drawPixel(i + DX2, Y1 + 13, 0);
		}

		for (i = X2; i <= X2
				+ (64 * rightsp / rightMaxhp); i++) {
			for (j = Y1; j <= Y1 + 10; j++) {
				g.drawPixel(i + DX2, j, 0);
			}
		}

		if (rightMaxfp > 0) {
			for (i = X2; i <= X2
					+ (64 * (rightfp < rightMaxfp ? rightfp
					: rightMaxfp) / rightMaxfp); i++) {
				for (j = Y1 + 16; j <= Y1 + 26; j++) {
					g.drawPixel(i + DX2, j, 0);
				}
			}
			for (i = X2; i <= X2 + 64; i++) {
				g.drawPixel(i + DX2, Y1 + 29, 0);
			}
		}

//		if(right.getDz() > 0)
//			g.drawPixmap(Assets.transfixed, 250, 30);

		y = 99;
		if (networkMode == NetworkMode.LOCAL) {
			if (right.getStr_bouns() != 0) {
				String att = right.getStr_bouns() > 0 ? "膂力+" : "膂力";
				g.drawText(att + right.getStr_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getAgi_bouns() != 0) {
				String att = right.getAgi_bouns() > 0 ? "敏捷+" : "敏捷";
				g.drawText(att + right.getAgi_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getWxg_bouns() != 0) {
				String att = right.getWxg_bouns() > 0 ? "悟性+" : "悟性";
				g.drawText(att + right.getWxg_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getBon_bouns() != 0) {
				String att = right.getBon_bouns() > 0 ? "根骨+" : "根骨";
				g.drawText(att + right.getBon_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getAtk_bouns() != 0) {
				String att = right.getAtk_bouns() > 0 ? "攻击+" : "攻击";
				g.drawText(att + right.getAtk_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getDef_bouns() != 0) {
				String att = right.getDef_bouns() > 0 ? "防御+" : "防御";
				g.drawText(att + right.getDef_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getHit_bouns() != 0) {
				String att = right.getHit_bouns() > 0 ? "命中+" : "命中";
				g.drawText(att + right.getHit_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
				y += 12;
			}
			if (right.getEvd_bouns() != 0) {
				String att = right.getEvd_bouns() > 0 ? "回避+" : "回避";
				g.drawText(att + right.getEvd_bouns(), 255 + DX2 - 30, y, FontSize.FT_12PX);
			}
		}
		final int GAUGE_LENGTH = 22;
		final int GAUGE_WIDTH = 9;
		final int GAUGE_MARGIN = 4;
		final int GAUGE_PADDING = 2;

		for (i = 1; i <= leftGauge; i++) {
			final int p = (i - 1) * (GAUGE_WIDTH + GAUGE_PADDING) + GAUGE_MARGIN;
			for (j = GAUGE_MARGIN; j <= GAUGE_MARGIN + GAUGE_LENGTH; j++) {
				for (int k = p; k <= p + GAUGE_WIDTH; k++) {
					g.drawPixel(j, k, 0);
				}
			}
		}

		for (i = 1; i <= rightGauge; i++) {
			final int p = (i - 1) * (GAUGE_WIDTH + GAUGE_PADDING) + GAUGE_MARGIN;
			for (j = GameConstants.FBWIDTH - GAUGE_MARGIN; j >= GameConstants.FBWIDTH - GAUGE_MARGIN - GAUGE_LENGTH; j--) {
				for (int k = p; k <= p + GAUGE_WIDTH; k++) {
					g.drawPixel(j, k, 0);
				}
			}
		}
	}

	public void setStatus(Status s) {
		Logger.i(s.toString());
		clear();
		s.pushToBattle();
	}

	/*
	 * （非 Javadoc）
	 *
	 * @see lostland.gmud.exv2.blgframework.CScreen#pause()
	 */
	@Override
	public void pause() {
	}

	/*
	 * （非 Javadoc）
	 *
	 * @see lostland.gmud.exv2.blgframework.CScreen#resume()
	 */
	@Override
	public void resume() {

		getGame().changeBGM(Assets.bgm_battle);
	}

	/*
	 * （非 Javadoc）
	 *
	 * @see lostland.gmud.exv2.blgframework.CScreen#dispose()
	 */
	@Override
	public void dispose() {
	}

	public static String bsp(Npc zdp, Npc bdp, String s, Item itm) {

		if (itm != null) {
			String t = s;
			t = t.replace("$N", zdp.name);
			t = t.replace("$n", bdp.name);
			t = t.replace("$w", zdp.getAttackItem().getName());
			t = t.replace("$W", bdp.getAttackItem().getName());
			t = t.replace("$i", itm.getName());
			String p[] = {"胸前", "后背", "左臂", "右臂", "左手", "右手", "左脚", "右脚", "头部",
					"小腹", "左肩", "右肩", "裆部", "臀部"};
			int i = (int) (Math.random() * p.length);
			t = t.replace("$1", p[i]);
			return t;
		} else
			return bsp(zdp, bdp, s);
	}

	public static String bsp(Npc zdp, Npc bdp, String s) {
		String t = s;
		t = t.replace("$N", zdp.name);
		t = t.replace("$n", bdp.name);
		t = t.replace("$w", zdp.getAttackItem().getName());
		t = t.replace("$W", bdp.getAttackItem().getName());
		String p[] = {"胸前", "后背", "左臂", "右臂", "左手", "右手", "左脚", "右脚", "头部",
				"小腹", "左肩", "右肩", "裆部", "臀部"};
		int i = (int) (Math.random() * p.length);
		t = t.replace("$1", p[i]);
		return t;
	}

	public static String bsp(Npc zdp, Npc bdp, ArrayList<String> ss) {
		return bsp(zdp, bdp, ss.get((int) (Math.random() * ss.size())));
	}

	public void xiqiprocess(Npc zdp, Npc bdp) {
		int t = zdp.getFp();
		zdp.addGauge(-GameConstants.XIQI_GAUGE_COST);
		zdp.xiqi();
		String pf = "";

		if (zdp.haveTalent(124) && Gmud.rand.nextBoolean()) // 天赋效果124
		{
			zdp.setFp(t);
			pf += "【至隐无名】";
		}

		new FreeStatus(zdp, bdp).pushToBattle();
		new ViewScreen(pf + zdp.name + "深深吸了口气，脸色看起来好多了。").pushToGame();
	}

	public void atkprocess(Npc zdp, Npc bdp, Gesture ges, Status controller) {
		if (controller != null) {
			controller.pushToBattle();
		}
		new PreAttackStatus(zdp, bdp, ges).pushToBattle();
	}

	public void atkprocess(Npc zdp, Npc bdp, Gesture ges, Status controller, String prefix) {

//		if(controller == null)
//		{
//			FreeStatus.fj = true;
//		}
//		else
//		{
//			FreeStatus.fj = false;
//		}

		if (controller != null) {
			controller.pushToBattle();
		}
		new PreAttackStatus(zdp, bdp, ges, prefix).pushToBattle();
	}

	public void taopaoprocess(Npc p, String text) {
		eob = true;
		BattleOverScreen.setTPFlag(true);
//		if(p.id >= 0)
//			p.weak = -15;
		new ViewScreen(text).pushToGame();
	}

	public Status pop() {
		return stack.remove(stack.size() - 1);
	}

	public void clear() {
		stack.clear();
	}


	public static void battle(int p2) {
		battle(-1, p2, new CommonBattle());
	}


	public static void battle(int p1, int p2, BattleHolder holder) {
		BattleScreen.getInstance().networkMode = NetworkMode.LOCAL;

		BattleScreen.getInstance().holder = holder;
		BattleScreen.getInstance().leftid = p1;
		BattleScreen.getInstance().enemyid = p2;
		BattleScreen.getInstance().eob = false;
		BattleScreen.getInstance().trust = false;
		BattleScreen.getInstance().qkdny1 = false;
		BattleScreen.getInstance().setStatus(new BattleStart((p1 >= 0) ? Gmud.getNpc(p1) : Gmud.mc, Gmud.getNpc(p2)));
		if (holder != null) {
			holder.pushToGame();
		}
		BattleScreen.getInstance().pushToGame();

		getInstance().p1().setBattleData(BattleScreen.getInstance().bd1);
		getInstance().p2().setBattleData(BattleScreen.getInstance().bd2);
		getInstance().p1().getBattleData().refresh();
		getInstance().p2().getBattleData().refresh();


	}


	public Npc p1() {
		return (leftid >= 0) ? Gmud.getNpc(leftid) : Gmud.mc;
	}

	public Npc p2() {
		return Gmud.getNpc(enemyid);
	}

	public BattleStateInfo getBsi() {
		return bsi;
	}

	public void setBsi(BattleStateInfo bsi) {
		this.bsi = bsi;
		p1().addGauge(-p1().getGauge());
		p1().addGauge(bsi.client_gauge);
		p1().setSp(bsi.client_sp);
		p1().setHp(bsi.client_hp);
		p1().setFp(bsi.client_fp);
	}


}
